﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TasksLibrary.Tasks
{
    public delegate void EventTaskCompleteHandler(ATask Task);
    public delegate void EventTaskTimeoutHandler(ATask Task);

    public abstract class ATask
    {
        #region Init
        protected abstract void Init(string name, int idparenttask);
        protected virtual void Clear()
        {
            TaskComplete = null;
            TaskTimeout = null;
        }
        #endregion

        #region Events
        public event EventTaskCompleteHandler TaskComplete;
        public event EventTaskCompleteHandler PeriodicTaskComplete;
        public event EventTaskTimeoutHandler TaskTimeout;

        public abstract bool CheckTimeout(int Timeout);
        public abstract bool CheckComplete();

        public abstract void TryCloseByComplete();
        public abstract void TryCloseByTimeout(int Timeout);

        protected void CallTaskComplete()
        {
            if (TaskComplete != null) TaskComplete(this);
            if (PeriodicTaskComplete != null) PeriodicTaskComplete(this);
        }

        protected void CallTaskTimeout()
        {
            if (TaskTimeout != null) TaskTimeout(this);
        }

        #endregion

        #region Property
        public int Id
        {
            get;
            protected set;
        }

        public int IdParentTask
        {
            get;
            protected set;
        }

        public string Name
        {
            get;
            protected set;
        }

        public DateTime DateCreated
        {
            get;
            protected set;
        }

        public bool IsPeriodicTask
        {
            get;
            set;
        }

        public TaskableDsspServiceBase Service
        {
            get;
            set;
        }
        #endregion

        #region Messages
        public abstract bool AddMessage<TMessType>(int countMessages = 1, EventTaskMessageAdding<TMessType> Delegate = null, bool AnySender = true) where TMessType : new();
        public abstract bool TryMessageComplete(object Message);
        public abstract TaskMessage<TType> GetMessage<TType>() where TType: new();
        //public abstract TaskMessage GetMessage<TType>();
        #endregion
    }

}
